import { getCurrentTimeParam } from "./globalTimeFilter.js";
import { API_CONFIG } from "./config.js";

document.addEventListener("DOMContentLoaded", function () {
    // 获取热门问答容器
    const hotQAContainer = document.getElementById("hotQA");
    if (!hotQAContainer) return;

    // 创建新的热门问答结构
    const hotQAHTML = `
        <div class="hot-qa-container">
            <div class="hot-qa-header">
                <div class="add">+</div>
            </div>
            
            <div class="hot-qa-content">
                <div class="scroll-container">
                    <div class="qa-list">
                        <!-- 问答内容将通过JS动态生成 -->
                    </div>
                </div>
            </div>
        </div>
    `;

    // 插入到容器中
    hotQAContainer.innerHTML = hotQAHTML;

    // 添加样式
    const style = document.createElement("style");
    style.innerHTML = `
        .hot-qa-container {
            // background: rgba(6, 33, 68, 0.7);
            border-radius: 8px;
            border: 1px solid rgba(36, 187, 236, 0.2);
            padding: 15px;
            box-shadow: 0 8px 32px rgba(2, 20, 42, 0.5);
            height: 100%; /* 占据整个容器高度 */
            display: flex;
            flex-direction: column;
        }
        
        .hot-qa-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 1px solid rgba(36, 187, 236, 0.3);
        }
        
        .hot-qa-header h2 {
            font-size: 1.6rem;
            font-weight: 600;
            color: #24bbec;
            position: relative;
        }
        
        .hot-qa-header h2::after {
            content: '';
            position: absolute;
            bottom: -11px;
            left: 0;
            width: 60px;
            height: 3px;
            background: linear-gradient(90deg, #24bbec, transparent);
            border-radius: 3px;
        }
        
        .hot-qa-header .add {
            color: #24bbec;
            font-size: 1.8rem;
            cursor: pointer;
        }
        
        .hot-qa-content {
            height: 600px; /* 原为 450px */
            overflow: hidden;
            position: relative;
            border-radius: 6px;
            border: 1px solid rgba(36, 187, 236, 0.15);
        }

        
        .scroll-container {
            height: 93%;
            overflow-y: auto;
            padding-right: 5px;
            margin-top: 7%;
        }
        
        .scroll-container::-webkit-scrollbar {
            width: 6px;
        }
        
        .scroll-container::-webkit-scrollbar-thumb {
            background-color: #24bbec;
            border-radius: 3px;
        }
        
        .qa-list {
            padding: 10px;
        }
        
        .qa-item {
            background: rgba(29, 111, 160, 0.2);
            border-radius: 8px;
            padding: 12px 15px;
            margin-bottom: 12px;
            border: 1px solid rgba(36, 187, 236, 0.15);
            transition: all 0.3s ease;
        }
        
        .qa-item:hover {
            background: rgba(36, 187, 236, 0.25);
            transform: translateY(-3px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
        }
        
        .question {
            font-weight: 600;
            margin-bottom: 8px;
            color: #24bbec;
            display: flex;
            align-items: center;
        }
        
        .question::before {
            content: 'Q:';
            display: inline-block;
            margin-right: 8px;
            background: #24bbec;
            color: #fff;
            width: 24px;
            height: 24px;
            border-radius: 50%;
            text-align: center;
            line-height: 24px;
            font-weight: bold;
        }
        
        .answer {
            color: #e6f7ff;
            line-height: 1.6;
            display: flex;
        }
        
        .answer::before {
            content: 'A:';
            display: inline-block;
            margin-right: 8px;
            background: #ffc640;
            color: #000;
            width: 24px;
            height: 24px;
            border-radius: 50%;
            text-align: center;
            line-height: 24px;
            font-weight: bold;
            flex-shrink: 0;
        }
        
        .hot-qa-stats {
            display: flex;
            justify-content: space-between;
            margin-top: 15px;
        }
        
        .stat-item {
            text-align: center;
            background: rgba(10, 58, 124, 0.4);
            border-radius: 6px;
            padding: 10px;
            flex: 1;
            margin: 0 5px;
            border: 1px solid rgba(36, 187, 236, 0.15);
        }
        
        .stat-value {
            font-size: 1.8rem;
            font-weight: 700;
            background: linear-gradient(to right, #e6f7ff, #24bbec);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .stat-label {
            color: #a2d3e8;
            font-size: 0.9rem;
        }
    `;
    document.head.appendChild(style);

    // 生成热门问答数据
    generateHotQA();

    // 添加滚动效果
    setupScrollEffect();
});

function generateHotQA() {
    const id = "gfwqsdfaca044c1b0dbb1dessdgsegc"; // 图片中的固定ID
    const type = "3"; // 图片中的固定type
    // 获取时间参数并格式化为YYYY-MM-DD
    const rawTime = getCurrentTimeParam();


    const params = new URLSearchParams();
    params.append("id", id);
    params.append("time", rawTime); // 使用格式化后的时间
    params.append("type", type);

    // 构建URL
    const url = `${
        API_CONFIG.BASE_URL
    }/api/metrics/${id}/single-metrics-value?${params.toString()}`;
    axios
        .get(url)
        .then((response) => {
            console.log("获取热门问答数据成功:", response.data);
            const hotQA = response.data.data;
            const qaList = document.querySelector(".qa-list");
            if (!qaList) return;

            // 清空现有内容
            qaList.innerHTML = "";

            // 生成问答列表
            hotQA.forEach((qa, index) => {
                const qaItem = document.createElement("div");
                qaItem.className = "qa-item";
                qaItem.innerHTML = `
                    <div class="question">${qa.question}</div>
                    <div class="answer">${qa.answer}</div>
                `;
                qaList.appendChild(qaItem);
            });
        })
        .catch((error) => {
            console.error("获取热门问答数据失败:", error);
        });
}

// 监听时间变化事件
document.addEventListener("timePeriodChanged", function () {
    generateHotQA();
});

// 设置滚动效果
function setupScrollEffect() {
    const scrollContainer = document.querySelector(".scroll-container");
    if (!scrollContainer) return;

    // 初始滚动位置
    let scrollPosition = 0;

    // 滚动函数
    function scrollQA() {
        scrollPosition += 1;
        if (
            scrollPosition >=
            scrollContainer.scrollHeight - scrollContainer.clientHeight
        ) {
            scrollPosition = 0;
        }
        scrollContainer.scrollTop = scrollPosition;
    }

    // 每50毫秒滚动一次
    setInterval(scrollQA, 50);

    // 鼠标悬停暂停滚动
    scrollContainer.addEventListener("mouseenter", () => {
        clearInterval(scrollInterval);
    });

    scrollContainer.addEventListener("mouseleave", () => {
        scrollInterval = setInterval(scrollQA, 50);
    });

    let scrollInterval = setInterval(scrollQA, 50);
}
